const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')

const typeDefs = gql`
  #学生类型
  type Student {
    sname: String
    age: Int
    gender: Boolean
  }
  #查询类型
  type Query {
    hello: String
    stu(id: Int, sname: String): Student
  }
`

const resolvers = {
  Query: {
    hello: () => 'Hello world!111',
    stu: (parent, args) => {
      console.log(parent, args)
      let stu = null
      if (args.id === 1 && args.sname === '李四') {
        stu = {
          sname: 'lisi',
          age: 12,
          gender: true
        }
      } else {
        stu = {
          sname: '---',
          age: 0,
          gender: false
        }
      }
      return stu
    }
  }
}

const server = new ApolloServer({ typeDefs, resolvers })

const app = express()
server.applyMiddleware({ app })

app.listen({ port: 4000 }, () => console.log('Now browse to http://localhost:4000' + server.graphqlPath))
/**
query param{
  hello
  stu(id:2,sname:"李四"){
    sname
    age
    gender
  }
}
 */
